package com.demo.advice;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ClassName MyAdvice
 * @Description TODO
 * @Author Windy
 * @Date 2022/9/6 16:45
 * @Version 1.0
 **/
@ControllerAdvice
public class MyAdvice {
    /**
     * 空指针异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = {NullPointerException.class})
    public ModelAndView NullPointer(Exception e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",e.toString());
        modelAndView.setViewName("error");
        return modelAndView;
    }

    /**
     * 算术异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = {ArithmeticException.class})
    public ModelAndView arithmeticException(Exception e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",e.toString());
        modelAndView.setViewName("error");
        return modelAndView;
    }

    /**
     * 下标越界异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = {ArrayIndexOutOfBoundsException.class})
    public ModelAndView arrayIndexOutOfBoundsException(Exception e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",e.toString());
        modelAndView.setViewName("error");
        return modelAndView;
    }

}
